import { loginApi } from './serve.js';

const msg = (title) => {
    uni.showToast({
        icon: 'none',
        title
    });
};

const verifyStatus = (status) => {
    let status_arr = {
        verify: '审核中',
        hidden: '已驳回',
        normal: '已通过'
    };
    return status_arr[status];
};

const auth = (url) => {
    if (!url) {
        msg('暂未开放！');
        return;
    }
    uni.navigateTo({
        url
    });
};

const authUrl = (url) => {
    if (!url) {
        msg('暂未开放！');
        return;
    }
    let token = uni.getStorageSync('token');
    // 跳转登录
    // if(!token){
    // 	url = "/pages/login/login"
    // }
    uni.navigateTo({
        url
    });
};

const authTab = (url) => {
    uni.switchTab({
        url
    });
};

const authReLaunch = (url) => {
    uni.reLaunch({
        url
    });
};

const authBack = (index = 1) => {
    uni.navigateBack({
        delta: index
    });
};

const authRedirectTo = (url) => {
    uni.redirectTo({
        url
    });
};

async function getUserInfo() {
    let token = uni.getStorageSync('token');
    if (!token) {
        url = '/pages/login/login';
        uni.navigateTo({
            url
        });
        return;
    }
    let userinfo = uni.getStorageSync('userInfo');
    if (!userinfo) {
        userinfo = await loginApi.getUserInfo();
        uni.setStorageSync('userInfo', userinfo);
    }
    return userinfo;
}

export default {
    auth,
    authUrl,
    authTab,
    authReLaunch,
    msg,
    authBack,
    authRedirectTo,
    getUserInfo,
    verifyStatus
};
